﻿#include "MySqlConnectPool.h"
//#include "read_system_config.h"

CMySqlConnectPool::CMySqlConnectPool(void)
{
    curSize = 0; // 当前已建立的数据库连接数量
    maxSize = 0; // MySql连接池中定义的最大数据库连接数
}

CMySqlConnectPool::~CMySqlConnectPool(void)
{
    DestoryConnPool();
}

CMySqlConnectPool *CMySqlConnectPool::Instance()
{
    static CMySqlConnectPool tMysqlPool;
    return &tMysqlPool;
}

MySqlApi *CMySqlConnectPool::CreateConnection()
{
    // const char *user = system_config::instance()->m_databaseInfo.db_user.c_str();
    // const char *pswd = system_config::instance()->m_databaseInfo.db_pwd.c_str();
    // const char *host = system_config::instance()->m_databaseInfo.db_ip.c_str();
    // const char *table = system_config::instance()->m_databaseInfo.db_dbname.c_str();
    // unsigned int port = system_config::instance()->m_databaseInfo.port;

    const char *host = "192.168.203.144";
    unsigned int port = 3306;
    const char *user = "root";
    const char *pswd = "0416";
    const char *table = "opcua";

    MySqlApi *conn = new MySqlApi(host, port, table, user, pswd);

    bool flag = conn->ConnDB(); // 连接成功；
    if (!flag)
    {
        printf("connect database failed\n");
        delete conn;
        return NULL;
    }
    return conn;
}

void CMySqlConnectPool::InitConnection(int iInitialSize)
{
    ScopedLock lock(m_tcpMutex);

    if (iInitialSize <= 0)
    {
        iInitialSize = 30;
    }

    maxSize = iInitialSize;

    MySqlApi *conn = NULL;
    for (int i = 0; i < iInitialSize; i++)
    {
        conn = this->CreateConnection();
        if (conn != NULL)
        {
            // printf("连接成功[%d]\n" , i);
            connList.push_back(conn);
            ++(this->curSize);
        }
        else
        {
            printf("connect failed [%d]\n", i);
        }
    }

    printf("MySql connect finshed\n");
}

void CMySqlConnectPool::DestoryConnection(MySqlApi *conn)
{
    ScopedLock lock(m_tcpMutex);
    if (conn != NULL)
    {
        delete conn;
        conn = NULL;
    }
}

void CMySqlConnectPool::DestoryConnPool()
{
    ScopedLock lock(m_tcpMutex);

    list<MySqlApi *>::iterator icon;

    char str[128] = {0};
    sprintf(str, "将要释放连接,大小[%d]", connList.size());
    // OutputDebugString(str) ;

    int i = 0;
    for (icon = connList.begin(); icon != connList.end(); ++icon)
    {
        this->DestoryConnection(*icon); // 销毁连接池中的连接
        i++;
        char str[128] = {0};
        sprintf(str, "释放第[%d]个连接", i);
        // OutputDebugString(str) ;
    }
    curSize = 0;
    connList.clear(); // 清空连接池中的连接
}

MySqlApi *CMySqlConnectPool::GetConnection()
{
    ScopedLock lock(m_tcpMutex);

    MySqlApi *con;
    if (connList.size() > 0)
    {
        // 连接池容器中还有连接
        con = connList.front(); // 得到第一个连接
        connList.pop_front();	// 移除第一个连接

        int icode = con->IsClose();
        if (icode) // 如果连接已经被关闭，删除后重新建立一个
        {
            delete con;
            con = NULL;

            con = this->CreateConnection();
        }

        // 如果连接为空，则创建连接出错
        if (con == NULL)
        {
            --curSize;
        }
        return con;
    }
    else
    {
        if (curSize < maxSize)
        {
            // 还可以创建新的连接
            con = this->CreateConnection();
            if (con != NULL)
            {
                ++curSize;
                return con;
            }
            else
            {
                return NULL;
            }
        }
        else
        {
            // 建立的连接数已经用到  maxSize ； 木有空闲可用；
            return NULL;
        }
    }
}

void CMySqlConnectPool::ReleaseConnection(MySqlApi *conn)
{
    ScopedLock lock(m_tcpMutex);

    if (conn != NULL)
    {
        connList.push_back(conn);
    }
}
